package com.are.odisea;

import java.sql.ResultSet;
import java.sql.SQLException;

public class ManejadorConfiguracion {
	private db conexion = null;
	private Configuracion conf;
	public db getConexion() {
		return conexion;
	}
	public void setConexion(db conexion) {
		this.conexion = conexion;
	}
	public Configuracion getConf() {
		return conf;
	}
	public void setConf(Configuracion conf) {
		this.conf = conf;
	}
	
	public ManejadorConfiguracion(db conexion) {
		super();
		this.setConexion(conexion);
		this.setConf(new Configuracion());
	}
	public ManejadorConfiguracion(db conexion, Configuracion conf) {
		super();
		this.setConexion(conexion);
		this.setConf(conf);
	}
	public boolean add() throws SQLException{
		boolean result = false;

			String sql = "insert into Configuracion (Car_adj,Carp_doc,Carp_img,Email,Extenciones,Max_documentos_band,Max_mensajes_band,Max_noticias_band,Max_tam_arch,Carp_evidencia_cap) values (?,?,?,?,?,?,?,?,?,?)";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, conf.getCar_adj());
			pst.setString(2, conf.getCarp_doc());
			pst.setString(3, conf.getCarp_imag());
			pst.setString(4, conf.getEmail());
			pst.setString(5, conf.getExtenciones());
			pst.setString(6, conf.getMax_documentos_band());
			pst.setString(7, conf.getMax_mensajes_band());
			pst.setString(8, conf.getMax_noticias_band());
			pst.setString(9, conf.getMax_tam_arch());
			pst.setString(10, conf.getCarp_evidencia_cap());
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;
	}
	
	public boolean update(String key) throws SQLException{
		boolean result = false;

			String sql = "update configuracion set Car_adj=?,Carp_doc=?,Carp_img=?,Email=?,Extenciones=?,Max_documentos_band=?,Max_mensajes_band=?,Max_noticias_band=?,Max_tam_arch=?,Carp_evidencia_cap=? where id=?";
			
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, conf.getCar_adj());
			pst.setString(2, conf.getCarp_doc());
			pst.setString(3, conf.getCarp_imag());
			pst.setString(4, conf.getEmail());
			pst.setString(5, conf.getExtenciones());
			pst.setString(6, conf.getMax_documentos_band());
			pst.setString(7, conf.getMax_mensajes_band());
			pst.setString(8, conf.getMax_noticias_band());
			pst.setString(9, conf.getMax_tam_arch());
			pst.setString(10, conf.getCarp_evidencia_cap());
			pst.setString(11, key);
			
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
			
		return result;
	}
	
	public boolean remove(String key) throws SQLException {
		boolean result = false;

			String sql = "delete from configuracion where id=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
			

		return result;	
	}
	
	
	public boolean exist(String key) throws SQLException {
		boolean result = false;

			String sql = "select * from configuracion where id = ?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				result = true;
			}
			rs.close();
		return result;	
	}
	
	public boolean Find(String key) throws SQLException {
		boolean result = false;

			String sql = "select * from configuracion where id=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				conf = new Configuracion();
				conf.setCar_adj(rs.getString("Car_adj"));
				conf.setCarp_doc(rs.getString("Carp_doc"));
				conf.setCarp_imag(rs.getString("Carp_img"));
				conf.setEmail(rs.getString("Email"));
				conf.setExtenciones(rs.getString("Extenciones"));
				conf.setMax_documentos_band(rs.getString("Max_documentos_band"));
				conf.setMax_mensajes_band(rs.getString("Max_mensajes_band"));
				conf.setMax_noticias_band(rs.getString("Max_noticias_band"));
				conf.setMax_tam_arch(rs.getString("Max_tam_arch"));
				conf.setId(rs.getString("id"));
				conf.setCarp_evidencia_cap(rs.getString("Carp_evidencia_cap"));
				result = true;
			}
			rs.close();
		return result;	
	}
	
	public String getFolderDocuments() throws SQLException {
		String folder ="";
		String sql = "select * from configuracion limit 1";
		ResultSet rs = conexion.Query(sql);
		if (rs.next()) {
			folder = rs.getString("Carp_doc");
		}
		
		return folder;
	}
}
